package com.example.watertest;



import java.io.IOException;

import org.andengine.audio.music.Music;
import org.andengine.audio.music.MusicFactory;
import org.andengine.audio.sound.Sound;
import org.andengine.audio.sound.SoundFactory;
import org.andengine.engine.Engine;
import org.andengine.engine.FixedStepEngine;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.physics.PhysicsHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.WakeLockOptions;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.IEntityModifier;
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierMatcher;
import org.andengine.entity.modifier.JumpModifier;
import org.andengine.entity.modifier.LoopEntityModifier;
import org.andengine.entity.modifier.ScaleModifier;
import org.andengine.entity.modifier.SequenceEntityModifier;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.extension.physics.box2d.FixedStepPhysicsWorld;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.extension.physics.box2d.util.Vector2Pool;
import org.andengine.input.sensor.acceleration.AccelerationData;
import org.andengine.input.sensor.acceleration.IAccelerationListener;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.util.GLState;
import org.andengine.ui.activity.BaseGameActivity;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.adt.color.Color;
import org.andengine.util.modifier.IModifier;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;

import android.hardware.SensorManager;
import android.util.Log;




public class PhysicsApplicationI extends BaseGameActivity implements IAccelerationListener, IOnSceneTouchListener {

	 public static int cameraWidth = 800;
	  public static int cameraHeight = 480;
	  public Scene mScene;
	  public PhysicsWorld mPhysicsWorld;
	  public Body groundWallBody;
	  public Body roofWallBody;
	  public Body leftWallBody;
	  public Body rightWallBody;
	private OnPopulateSceneCallback pOnPopulateSceneCallback; 
	
	  @Override
	  public Engine onCreateEngine(final EngineOptions
	      pEngineOptions) {
	    return new FixedStepEngine(pEngineOptions, 60);
	  }

	  @Override
	  public EngineOptions onCreateEngineOptions() {
	    EngineOptions engineOptions = new EngineOptions(true,
	      ScreenOrientation.LANDSCAPE_SENSOR, new
	        FillResolutionPolicy(), new Camera(0,
	          0, cameraWidth, cameraHeight));
	    engineOptions.getRenderOptions().setDithering(true);
	    engineOptions.getRenderOptions().
	      getConfigChooserOptions()
	        .setRequestedMultiSampling(true);
	    engineOptions.setWakeLockOptions(
	      WakeLockOptions.SCREEN_ON);
	    return engineOptions;
	  }

	  @Override
	  public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) {
	    pOnCreateResourcesCallback.
	      onCreateResourcesFinished();
	  }

	  @Override
	  public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) {
	    mScene = new Scene();
	    mScene.setBackground(new Background(0.9f,0.9f,0.9f));
	    pOnCreateSceneCallback.onCreateSceneFinished(mScene);
	    
	   
	    
	    
	    
	  }
	  
	  @Override
	  public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) {
		  
		  mPhysicsWorld = new PhysicsWorld(new Vector2(0,-10), false);
		  		mScene.registerUpdateHandler(mPhysicsWorld);
		  		FixtureDef BoxBodyFixtureDef = PhysicsFactory.createFixtureDef(20f, 1f, 0.5f);	
		  
		  		Rectangle staticRectangle = new Rectangle(cameraWidth / 2f,75f,400f,40f,this.getVertexBufferObjectManager());
		  		staticRectangle.setColor(0.8f, 0f, 0f);
				mScene.attachChild(staticRectangle);
				Body staticBody = PhysicsFactory.createBoxBody(mPhysicsWorld, staticRectangle, BodyType.StaticBody, BoxBodyFixtureDef);
				mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector( staticRectangle, staticBody));
				
				
				FixtureDef BoxBodyFixtureDefI = PhysicsFactory.createFixtureDef(2f, 0f, 0.5f);	
				Rectangle dynamicRectangle = new Rectangle(400f, 300f, 40f, 40f, this.getVertexBufferObjectManager());
				dynamicRectangle.setColor(0f, 0.8f, 0f);
				mScene.attachChild(dynamicRectangle);
				Body dynamicBody = PhysicsFactory.createBoxBody(mPhysicsWorld, dynamicRectangle, BodyType.DynamicBody, BoxBodyFixtureDef);
				mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector( dynamicRectangle, dynamicBody));

				FixtureDef BoxBodyFixtureDefII = PhysicsFactory.createFixtureDef(60f, 03f, 0.7f);
				Rectangle kinematicRectangle = new Rectangle(600f, 120f, 40f, 40f, this.getVertexBufferObjectManager());
				kinematicRectangle.setColor(0.8f, 0.8f, 0f);
				mScene.attachChild(kinematicRectangle);
				Body kinematicBody = PhysicsFactory.createBoxBody(mPhysicsWorld, kinematicRectangle, BodyType.KinematicBody, BoxBodyFixtureDef);
				mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector( kinematicRectangle, kinematicBody));
				
				kinematicBody.setLinearVelocity(-2f, 0f);
				kinematicBody.setAngularVelocity((float) (-Math.PI));
				mScene.setOnSceneTouchListener(this);
	    	    pOnPopulateSceneCallback.onPopulateSceneFinished();
				
	  }

	  @Override
	  public boolean onSceneTouchEvent(Scene pScene, TouchEvent
	      pSceneTouchEvent) {
	    // Further recipes in this chapter will require us
	      //to place code here.
	    return true;
	  }
	  
	  @Override
	  public void onAccelerationAccuracyChanged(
	      AccelerationData pAccelerationData) {}

	  @Override
	  public void onAccelerationChanged(
	      AccelerationData pAccelerationData) {
	    final Vector2 gravity = Vector2Pool.obtain(
	      pAccelerationData.getX(), 
	      pAccelerationData.getY());
	    this.mPhysicsWorld.setGravity(gravity);
	    Vector2Pool.recycle(gravity);
	  }

	  @Override
	  public void onResumeGame() {
	    super.onResumeGame();
	    this.enableAccelerationSensor(this);
	  }

	  @Override
	  public void onPauseGame() {
	    super.onPauseGame();
	    this.disableAccelerationSensor();
	  }
	
	
		
	
	
}
